//~ Project: wslds-blog File: ConfigDaoImpl.java Created date: Feb 27, 2012
package com.wslds.blog.dao;

import java.util.Collection;

import javax.annotation.Resource;

import com.ii2d.j2ee.plugin.config.Config;
import com.ii2d.j2ee.plugin.config.dao.AbstractConfigDao;
import com.ii2d.j2ee.plugin.config.dao.ConfigDao;
import com.wslds.base.dao.BaseDaoImpl;

/**
 * 
 * @author $Author: donilan.qq@gmail.com $
 * @version $Revision: 103 $
 */
public class ConfigDaoImpl extends AbstractConfigDao implements ConfigDao {

	public static final String TABLE = "config";
	
	@Resource private BaseDaoImpl dao;
	
	@Override
	public Collection<Config> findAll() {
		return dao.find(TABLE, null);
	}

	@Override
	protected Config doAddConfig(Config config) {
		dao.save(TABLE, config);
		return config;
	}

	@Override
	protected Config doRemoveConfig(String key) {
		Config config = new com.wslds.bean.Config();
		config.setKey(key);
		dao.delete(TABLE, config);
		return null;
	}

	@Override
	protected Config doUpdateConfig(Config config) {
		dao.update(TABLE, config);
		return config;
	}

	@Override
	protected Config doFindConfigByKey(String key) {
		Config config = new com.wslds.bean.Config();
		config.setKey(key);
		return dao.findOne(TABLE, config);
	}

	@Override
	public Config doAddConfig(String key, String value) {
		Config config = new com.wslds.bean.Config();
		config.setKey(key);
		Config tmp = dao.findOne(TABLE, config);
		if(tmp != null) {
			return tmp;
		}
		config.setValue(value);
		config.setType("no-type");
		dao.save(TABLE, config);
		return config;
	}

	
}
